<?php


$dbhost="37.140.192.111";
$dbuser="u0014355_root";
$dbpass="passw0rd";
$dbname="u0014355_Database";
connectToDB();

/*
 * Settings
 */
function getSettings()
{
    $t =  mysql_query("SELECT * FROM aa_settings");
    for ($c=0; $c<mysql_num_rows($t); $c++)
    {
        $i = mysql_fetch_array($t);
        $SETTINGS["$i[1]"] = $i[2];
    }
    return $SETTINGS;
}

/*   ------     */

function connectToDB() {
  global $db, $dbhost, $dbuser, $dbpass, $dbname;
  ($db = mysql_pconnect("$dbhost", "$dbuser", "$dbpass")) || die("Couldn't connect to MySQL");
  mysql_select_db("$dbname", $db) || die("Couldn't open db: $dbname. Error if any was: ".mysql_error() );
  mysql_query("SET NAMES 'utf8'"); 
mysql_query("SET CHARACTER SET 'utf8'");
mysql_query("SET SESSION collation_connection = 'utf8_general_ci'");
} 


function redirect_php($url,$timer=0){ 
    echo '<meta http-equiv="refresh" content="'.$timer.'; url='.$url.'">'; 
}


function getIp()
{
  if (!empty($_SERVER['HTTP_CLIENT_IP']))
  {
    $ip=$_SERVER['HTTP_CLIENT_IP'];
  }
  elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
  {
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  }
  else
  {
    $ip=$_SERVER['REMOTE_ADDR'];
  }
  return $ip;
}

function randomString($length = 8)
{
    $chars = 'abdefhiknrstyzABDEFGHKNQRSTYZ23456789';
    $numChars = strlen($chars);
    $string = '';
    for ($i = 0; $i < $length; $i++) {
        $string .= substr($chars, rand(1, $numChars) - 1, 1);
    }
    return $string;
}

function randomDString($length = 9)
{
    $chars = '0123456789';
    $numChars = strlen($chars);
    $string = '';
    for ($i = 0; $i < $length; $i++) {
        $string .= substr($chars, rand(1, $numChars) - 1, 1);
    }
    return $string;
}


function getVacantPort ()
{
    $SETTINGS = getSettings();
    if ($SETTINGS['port_end'] < $SETTINGS['port_begin'])
    {
        return -1;
    }
    $t =  mysql_query("SELECT port FROM `accounts`  WHERE state != -1");
    for ($c=0; $c<mysql_num_rows($t); $c++)
    {
        $i = mysql_fetch_row($t);
        $z[$c] = $i[0];
    }
    for ($j = $SETTINGS['port_begin'] ; $j <= $SETTINGS['port_end'] ; $j++)
    {
        if (!in_array($j , $z))
        {
            return $j;
        }
    }
    return -1;
}


function suka ($id)
{


// достаем золото SELECT SUM( gold ) FROM  `aa_gold` WHERE user_id =1261

    $q = "SELECT
	SUM(gold) , ROUND (
	SUM(gold*(
		SELECT
			exchangeRate
		FROM
			aa_warehouses
		WHERE
		aa_warehouses.server =
			(
			SELECT server
			from accounts
			WHERE accounts.id = aa_gold.user_id
			)
		)
	) , 2 ) AS r
FROM
	`aa_gold`
WHERE user_id = ".$id;
    $money = mysql_query($q) or die("error 33");
    $money =  mysql_fetch_row($money);

// достаем золото SELECT SUM( gold ) FROM  `aa_gold` WHERE user_id =1261

	$query = "SELECT * ,  DATEDIFF( NOW( ) , create_date ) +1 FROM accounts WHERE id = ".$id."  LIMIT 1";
	$result = mysql_query($query) or die("error 33");
	for ($c=0; $c<mysql_num_rows($result); $c++)
    {
        $t = mysql_fetch_row($result);
    }
    return ($money[1]-($t[18]*15));

}



function logIn($login, $password) 
{
  session_start();
  $_SESSION["login"]=$login;
  $_SESSION["password"]=$password;
  $_SESSION["loggedIn"]=true;
  redirect_php('/'); 
} 


function logOut()
{
  session_destroy();
  redirect_php('/'); 
}

function getUser($_l , $_p)
{
  $q = "SELECT * FROM users WHERE login = \"$_l\" AND password = \"$_p\"";
  $res = mysql_query($q) or die("error 33");
  $t = mysql_fetch_row($res);
  return $t;
}

?>